#include "SimpleImage.hpp"


SimpleImage::SimpleImage()
{
    m_texture = 0;
    app_state = 0;

    x = y = w = h = 0;

    ::set_color(color, 255, 255, 255, 255);

    flip.x = 0;
    flip.y = 0;
}

SimpleImage::~SimpleImage()
{
    if (m_texture) {
        SDL_DestroyTexture(m_texture);
    }
}

SDL_Texture* SimpleImage::texture()
{
    return m_texture;
}

void SimpleImage::init(AppState* app_state)
{
    this->app_state = app_state;
}

void SimpleImage::create_text(const char* text)
{
    if (m_texture) {
        SDL_DestroyTexture(m_texture);
    }

    SDL_Color color = { 255, 255, 255, 255 };
    SDL_Surface* sf = TTF_RenderUTF8_Blended(app_state->font, text, color);
    if (sf == nullptr) {
        zj_logger_e("TTF_RenderUTF8_Blended错误：", "%s", TTF_GetError());
        return;
    }

    this->m_texture = SDL_CreateTextureFromSurface(app_state->renderer, sf);
    if (m_texture == nullptr)
        zj_logger_e("SDL_CreateTextureFromSurface错误：", "%s", SDL_GetError());

    SDL_FreeSurface(sf);
}

void SimpleImage::create_image(const char* path)
{
    if (m_texture) {
        SDL_DestroyTexture(m_texture);
    }

    m_texture = IMG_LoadTexture(app_state->renderer, path);
    if (m_texture == nullptr) {
        zj_logger_e("create_image错误:", "(path|%s) err: %s", path, SDL_GetError());
    }
}

void SimpleImage::create(SDL_Texture* texture)
{
    if (m_texture) {
        SDL_DestroyTexture(m_texture);
    }

    m_texture = texture;
}

void SimpleImage::set_color(int r, int g, int b, int a)
{
    ::set_color(color, r, g, b, a);
}

void SimpleImage::set_color(SDL_Color& color)
{
    ::set_color(this->color, color.r, color.g, color.b, color.a);
}

void SimpleImage::apply_color(SDL_Color& color)
{
    SDL_SetTextureColorMod(m_texture, color.r, color.g, color.b);
    SDL_SetTextureAlphaMod(m_texture, color.a);
}

SDL_RendererFlip SimpleImage::flip_state()
{
    return (SDL_RendererFlip)((flip.x>0 ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE) | (flip.y>0 ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE));
}

void SimpleImage::draw()
{
    int img_w, img_h;
    SDL_QueryTexture(m_texture, 0, 0, &img_w, &img_h);
    float w_div_h = 1.0*img_w/img_h;

    SDL_Rect rect;
    rect.w = w;
    rect.h = w/w_div_h;
    if (rect.h > h) {
        rect.h = h;
        rect.w = h*w_div_h;
    }
    rect.x = x+w/2 - rect.w/2;
    rect.y = y+h/2 - rect.h/2;

    apply_color(color);
    SDL_RenderCopyEx(app_state->renderer, m_texture, 0, &rect, 0, 0, flip_state());
}

void SimpleImage::draw(int x, int y, int w, int h, bool nottesting)
{
    if (nottesting) {
        this->x = x;
        this->y = y;
        this->w = w;
        this->h = h;
        draw();
    } else {
        draw(x, y, w, h);
    }
    //SDL_SetRenderDrawColor(app_state->renderer, 0, 0, 0, 255);
    //SDL_RenderDrawLine(app_state->renderer, x, y, x+w, y+h);
    //SDL_RenderDrawLine(app_state->renderer, x, y+h, x+w, y);
}

void SimpleImage::draw(int x, int y, int w, int h)
{
    SDL_Rect rect = { x, y, w, h };
    apply_color(color);
    SDL_RenderCopy(app_state->renderer, m_texture, 0, &rect);
}
